﻿package $${Obj.PackageName}$$.control;

import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.inspur.system.jslib.datatables.DataTableCondition;
import com.inspur.system.jslib.datatables.DataTableParameter;
import com.inspur.system.jslib.datatables.DataTableResult;
import com.inspur.system.repository.Page;
import com.inspur.system.tools.file.FileOperateUtil;
import $${Obj.PackageName}$$.model.$${Obj.ClassNameFirstUpper}$$;
import $${Obj.PackageName}$$.service.I$${Obj.ClassNameFirstUpper}$$Service;


/**
 * Controller - Spring
 * 
 * @author XXX
 */
@Controller("$${Obj.PackageName}$$.control.$${Obj.ClassNameFirstUpper}$$Controller")
@RequestMapping(value="/plugins/$${Obj.PluginName}$$/$${Obj.ClassNameAllLower}$$")
public class $${Obj.ClassNameFirstUpper}$$Controller  {

	@Autowired
	private I$${Obj.ClassNameFirstUpper}$$Service $${Obj.ClassNameFirstLower}$$Service;
	
	@RequestMapping(value="/findAll.ilf")
	@ResponseBody
	public List<$${Obj.ClassNameFirstUpper}$$> findAll() {
		return $${Obj.ClassNameFirstLower}$$Service.findAll();
	}
	
	@RequestMapping(value="/findOne.ilf")
	@ResponseBody
	public $${Obj.ClassNameFirstUpper}$$ findOne(@RequestParam("id") Long id) {
		return $${Obj.ClassNameFirstLower}$$Service.findOne(id);
	}
	
	@RequestMapping(value = "/findPage.ilf")
	@ResponseBody
	public DataTableResult<$${Obj.ClassNameFirstUpper}$$> findPage(
			@RequestParam String tableparam, @RequestParam String conditions)
			throws Exception {
		//处理参数
		DataTableParameter p = new DataTableParameter(tableparam);
		Page<$${Obj.ClassNameFirstUpper}$$> page = new Page<$${Obj.ClassNameFirstUpper}$$>();
		page.setStart(p.getStart());
		page.setLimit(p.getLimit());
		Map<String, String> orders = p.GetSort();
		Map<String, String> c = DataTableCondition.getCondition(conditions);
		page = $${Obj.ClassNameFirstLower}$$Service.findPageByCondition(page, c, orders);

		// 返回结果
		DataTableResult<$${Obj.ClassNameFirstUpper}$$> result = new DataTableResult<$${Obj.ClassNameFirstUpper}$$>();
		result.setAaData(page.getRows());
		result.setiTotalDisplayRecords(page.getTotalCount());
		result.setiTotalRecords(page.getTotalCount());
		result.setsEcho(p.getsEcho());
		return result;
	}

	@RequestMapping(value = "/export.ilf")
	@ResponseBody
	public void export(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String realName =request.getParameter("filename");
		String filetype = request.getParameter("filetype");
		String conditions = request.getParameter("conditions");
		Map<String, String> c = DataTableCondition.getCondition(conditions);
		String filePath = $${Obj.ClassNameFirstLower}$$Service.exportByCondition(filetype, c);
		FileOperateUtil.download(request,response,filePath,realName);
	}	
	##{SQL
$$<(ObjSql.SqlType=insert)
	@RequestMapping(value="/create.ilf")
	@ResponseBody
	public String create(@RequestBody $${Obj.ClassNameFirstUpper}$$ $${Obj.ClassNameFirstLower}$$) throws Exception {

		try{	
			$${Obj.ClassNameFirstLower}$$Service.create($${Obj.ClassNameFirstLower}$$);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	>$$
$$<(ObjSql.SqlType=update)
	@RequestMapping(value="/update.ilf")
	@ResponseBody
	public String update(@RequestBody $${Obj.ClassNameFirstUpper}$$ $${Obj.ClassNameFirstLower}$$) throws Exception {
		try{
			$${Obj.ClassNameFirstLower}$$Service.update($${Obj.ClassNameFirstLower}$$);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	>$$
$$<(ObjSql.SqlType=delete)
	@RequestMapping(value="/delete.ilf")
	@ResponseBody
	public String delete(@RequestBody $${Obj.ClassNameFirstUpper}$$ $${Obj.ClassNameFirstLower}$$) throws Exception {		
		try{
			$${Obj.ClassNameFirstLower}$$Service.delete($${Obj.ClassNameFirstLower}$$.get$${Obj.PkClassFieldNameFirstUpper}$$());
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	>$$
}##
}
